<template>
  <div class="MyBar" :style="{ width: width }">
    <div class="item_box" v-for="(item, index) in data" :key="index" v-show="index<=temp">
      <div class="left_boeder" :style="[
          { borderRight: '2px solid ' + color[index] + '!important' },
          { width: (item.value / maxNum) * 100 + '%' },
        ]">
        <span>{{ item.name }}</span>
        <span :style="[{ color: color[index] + '!important' },{textShadow: '1px 0px 3px ' + color[index]}]" style='margin-top:1px'>{{(item.value/maxNum*100).toFixed(2)}}%</span>
      </div>
      <span class="title">{{ item.value }}标准箱</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    data: {
      // 返回的数据
      type: Array | Object,
      default: function () {
        return [];
      },
    },
    width: {
      type: String,
      default: "100%",
    },
  },
  data() {
    return {
      color: [
        "#20ade9",
        "#03dac5",
        "#fc4ff0",
        "#1268ff",
        "#01b640",
        "#ffcb00",
        "#fe6f37",
        "#fc2a2a",
        "#20ade9",
        "#03dac5",
      ],
      yData: [],
      maxNum: 0,
      temp: 7,
    };
  },
  created() {
    this.yData = [];
    for (let item of this.data) {
      this.yData.push(item.value);
    }
    this.maxNum = Math.max(...this.yData) + 20;
  },
  mounted() {
    //表格自适应高度
    this.getAutoHeight();
    window.onresize = () => {
      this.getAutoHeight();
    };
  },
  methods: {
    //表格自适应高度
    getAutoHeight() {
      //   this.$nextTick(() => {
      // clearTimeout(settimeout);
      // var settimeout = setTimeout(() => {
      //   console.log(document.documentElement.clientHeight);
      this.temp = document.documentElement.clientHeight / 180;
      console.log(this.temp);
      //     }, 100);
      //   });
    },
  },
};
</script>

<style lang="scss" scoped>
.MyBar {
  width: 100%;
  .item_box {
    width: 100%;
    height: 25px;
    display: flex;
    align-items: center;
    // background: red;
    .left_boeder {
      border-bottom: 1px dashed #3072a2;
      border-left: 2px solid #07b9b5;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 15px;
      background: rgba(23, 71, 126, 0.9);
      height: 25px;
      max-width: 80%;
      span {
        font-size: 12px;
        transform: scale(0.9);
        color: #ffffff;
      }
    }
    .title {
      font-size: 12px;
      color: #acdbee !important;
      margin-left: 4px;
      transform: scale(0.85);
    }
  }
  .item_box:nth-child(1) .left_boeder {
    border-top: 1px dashed #3072a2;
  }
}
</style>